package com.example.springbootrbac.config;

import com.example.springbootrbac.Filter.SecurityTokenProcessFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.web.cors.CorsConfigurationSource;

import javax.annotation.Resource;

@Configuration
public class RbacWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Resource(name = "userDetailsServiceImpl")
    private UserDetailsService userDetailsService;

    @Resource(name = "corsConfigurationSource")
    private CorsConfigurationSource corsConfigurationSource;

    @Resource(name = "bCryptPasswordEncoder")
    private PasswordEncoder passwordEncoder;

    @Resource(name = "securityAuthenticationSuccessHandler")
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Resource(name = "securityAuthenticationFailureHandler")
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Resource(name = "securityAuthenticationEntryPointHandler")
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Resource(name = "securityAccessDeniedHandler")
    private AccessDeniedHandler accessDeniedHandler;

    @Resource(name = "securityTokenProcessFilter")
    private SecurityTokenProcessFilter securityTokenProcessFilter;


    @Autowired
    LogoutSuccessHandler logoutSuccessHandler;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // 使用自定义登录逻辑和加密算法
        auth
                .userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 关闭 csrf 防伪造请求
        http.csrf(AbstractHttpConfigurer::disable);

        // 启用表单登录
        http.formLogin(config -> config
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .permitAll()
        );
        // 退出处理
        http.logout(configurer -> configurer
                .logoutSuccessHandler(logoutSuccessHandler)
                .permitAll()
        );


        // 跨域配置
        http.cors(config -> config
                .configurationSource(corsConfigurationSource)
        );

        // 设置认证错误处理(未登录)
        http.exceptionHandling(config -> config
                .authenticationEntryPoint(authenticationEntryPoint)//先进行登录认证
                .accessDeniedHandler(accessDeniedHandler)//再进行权限认证 两者均可返回认证结果
        );

        // 自定义权限验证规则
        http.authorizeRequests(config -> config
                .anyRequest().access("@securityPermissionEvaluatorHandler.hasPermission(authentication,null ,null)")
        );

        // 添加自定义验证规则的拦截器
        http.addFilterBefore(securityTokenProcessFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
